<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house" style="font-weight: bold">&nbsp;&nbsp;后台管理</i>
        </el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-marketing">&nbsp;&nbsp;销售管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-circle-plus">&nbsp;&nbsp;客户管理</i></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div>
      <div class="data-canRead" style="width: 100%;background-color: white;height: auto">
        <el-divider content-position="left">数据添加</el-divider>
        <el-form
            :model="manageForm"
            :rules="rules"
            ref="manageForm"
            label-width="100px"
            style="margin-right: 25px">
          <el-form-item label="区域:" prop="area">
            <el-input v-model="manageForm.area" placeholder="请输入区域"></el-input>
          </el-form-item>
          <el-form-item label="企业名称:" prop="companyName">
            <el-input v-model="manageForm.companyName" placeholder="输入企业名称"></el-input>
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item label="联系人:" prop="contactType">
                <el-select v-model="manageForm.contactType" placeholder="请选择联系人">
                  <el-option
                      v-for="item in contactTypes"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="姓名:" prop="contactName">
                <el-input v-model="manageForm.contactName" placeholder="输入联系人姓名"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="联系电话:" prop="contactCell">
            <el-input v-model="manageForm.contactCell" placeholder="输入联系电话"></el-input>
          </el-form-item>
          <el-form-item label="详细地址:" prop="address">
            <el-input v-model="manageForm.address" placeholder="输入详细地址"></el-input>
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item label="主要产品:" prop="mainCategory">
                <el-input v-model="manageForm.mainCategory" placeholder="输入主要产品"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="来往方式:" prop="contactMethods">
                <el-select v-model="manageForm.contactMethods" placeholder="请选择来往方式">
                  <el-option
                      v-for="item in contactMethod"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="备注:" prop="remarks">
            <el-input v-model="manageForm.remarks" placeholder="输入备注"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('manageForm')" style="color: white">添加</el-button>
            <el-button type="warning" @click="resetForm('manageForm')" style="color: white">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      manageForm: {
        area: '京津',
        companyName: '达内',
        contactType: '老板',
        contactName: '阿金',
        contactCell: '14577350079',
        address: '滨海新区',
        mainCategory: '文具',
        contactMethods: '支付宝',
        remarks: '已结束交割'
      },
      contactTypes: [
        {
          label: "老板",
          value: "BOSS"
        },
        {
          label: "联系人",
          value: "Responser"
        }
      ],
      contactMethod: [
        {
          label: "支付宝",
          value: "zfb"
        },
        {
          label: "微信",
          value: "wechat"
        },
        {
          label: "公司账号",
          value: "campany"
        },
        {
          label: "转账",
          value: "zhuanzhang"
        },
        {
          label: "现金",
          value: "cash"
        },
      ],
      rules: {
        area: [
          {required: true, message: "请输入区域", trigger: 'change'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        companyName: [
          {required: true, message: "请输入企业名称", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        contactName: [
          {required: true, message: "请输入姓名", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        contactType: [
          {required: true, message: "请选择联系人", trigger: 'change'},
        ],
        contactCell: [
          {required: true, message: "请输入联系电话", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        address: [
          {required: true, message: "请输入详细地址", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        mainCategory: [
          {required: true, message: "请输入主要产品", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        contactMethods: [
          {required: true, message: "请选择来往方式", trigger: 'change'},
        ],
        remarks: [
          {required: true, message: "请输入备注", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9080/customManages/add-new';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.manageForm);
          console.log('formData = ' + formData);

          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            console.log(responseBody);
            if (responseBody.state == 20000) {
              this.$message({
                showClose: true,
                message: '添加成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message({
                showClose: true,
                message: responseBody.message,
                type: 'error'
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

  },

}

</script>

<style scoped>
* {
  color: black;
}

.data-canRead {
  width: 380px;
  height: 380px;
  margin-top: 15px;
  box-shadow: 0px 5px 5px #8d858a;
  float: left;
  margin-right: 16px;
}
</style>